#!/usr/bin/env python
#-*-coding:utf-8-*-

import os
import time
import threading
import subprocess

#def speech(language, text):
#  os.system('espeak --stdout -s100 -v'+language+' "'+text+'" | paplay')

class Espeak(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    self.lang = "hu"
    self.speed = 100
    self.text = ""
    self.start_flag = False
    self.run_flag = True
  def stop_speech(self):
    if self.start_flag:
      self.start_flag = False
      self.proc1.kill()
      self.proc2.kill()
  def exit_speech(self):
    self.stop_speech()
    self.run_flag = False
  def set_text(self, text):
    self.text = text
    self.start_flag = True
  def run(self):
    while self.run_flag:
      if self.start_flag:
        self.proc1 = subprocess.Popen(['espeak', '-m', '--stdout', '-v', self.lang, self.text], stdout=subprocess.PIPE)
        self.proc2 = subprocess.Popen(['paplay'], stdin=self.proc1.stdout)
        self.proc2.communicate()
      time.sleep(1)

